/*
 * Copyright 2016. chenshufei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sz.szsmk.citizencard.ui.dzsmkfragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentActivity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.ioc.dzsmkcompontent.DaggerFrgmentComponent;
import sz.szsmk.citizencard.ioc.dzsmkcompontent.FrgmentComponent;
import sz.szsmk.citizencard.ioc.dzsmkmodule.FragmentModule;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.retrofitrxjava.DZSmkAppInit;
import sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity;


public abstract class AppBaseFragment extends BaseFragment {

    private int mNavigationIcon = -1;
    private Drawable mNavigationDrawable;

    private Toolbar mToolbar;
    private TextView mTitleTextView;
    private ImageView mIv_center_log;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //mNavigationIcon = R.drawable.common_bg_top_back;

    }



    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        View toolView = view.findViewById(R.id.toolbar);
        if (null != toolView){
            mToolbar = (Toolbar) toolView;
            FragmentActivity activity = getActivity();
            if (activity instanceof AppCompatActivity){
                AppCompatActivity appActivity = (AppCompatActivity) activity;
                appActivity.setSupportActionBar(mToolbar);
            }



            mTitleTextView = (TextView) toolView.findViewById(R.id.tv_title);
            mIv_center_log = (ImageView) toolView.findViewById(R.id.iv_center_log);

            CharSequence title = getTopTitle();
            if (null != title && title.length()>0){
                mTitleTextView.setText(title);
            }

            if (-1 != mNavigationIcon){
                mToolbar.setNavigationIcon(mNavigationIcon);
            }

            if (null != mNavigationDrawable){
                mToolbar.setNavigationIcon(mNavigationDrawable);
            }
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickNavigationIcon();
                }
            });

        }
    }

    public FrgmentComponent getComponent(){
        FrgmentComponent component = DaggerFrgmentComponent.builder()
                .fragmentModule(new FragmentModule(this))
                .applicationComponent(DZSmkAppInit.getInstance().getComponent())
                .build();
        return component;
    }

    protected abstract CharSequence getTopTitle();

    protected void onClickNavigationIcon() {

    }

    public void setToolbarBackgroundColor(int toolBarBackgroundColor){
        mToolbar.setBackgroundColor(toolBarBackgroundColor);
    }

    //设置导航icon，通常都是返回icon
    protected void setNavigationIcon(int iconResourceId){
        mNavigationIcon = iconResourceId;
    }
    protected void setNavigationIcon(Drawable navigationDrawable){
        mNavigationDrawable = navigationDrawable;
    }

    protected void setDisableNavigationIcon(){
        mToolbar.setNavigationIcon(null);
    }

    protected void setCenterImageEnable(){
        mTitleTextView.setVisibility(View.GONE);
        mIv_center_log.setVisibility(View.VISIBLE);
    }

    public void onEvent(MessageEvent event) {
    }


    public <T extends View> T getViewById(View view, int id) {
        return (T) view.findViewById(id);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //DZSmkAppInit.getInstance().getRefWatcher().watch(this);
    }

    @Override
    public void onUnLogin() {
        super.onUnLogin();
        FragmentActivity activity = getActivity();
        if (activity instanceof AppBaseActivity){
            AppBaseActivity baseActivity = (AppBaseActivity) activity;
            baseActivity.onUnLogin();
        }
    }

    protected void showToast(String message) {
        Toast toast = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
        toast.show();
    }
}
